package com.itheima;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.Dao.UserDao;

import com.itheima.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.Map;

@SpringBootTest
public class Mybatis1 {
    @Autowired
    private UserDao userDao;

    @Test
    void testGetAll() {
        List<User> userList = userDao.selectList(null);
        System.out.println(userList);
    }

    @Test
    public void testGetAll1(){
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("char_length(name)",4); // 查询名字长度为4的信息
        List<User> users = userDao.selectList(qw);
        for (User user : users) {
            System.out.println(user);
        }
    }

    @Test
    public void testGetAll2(){
        //QueryWrapper<User> qw = new QueryWrapper<>();
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getName,"tom");
        List<User> users = userDao.selectList(qw);
        for (User user : users) {
            System.out.println(user);
        }
    }

    // 分页
    @Test
    public void testGetAll3(){
        Page<User> page = new Page<>();
        userDao.selectPage(page,null);
        long total = page.getTotal();
        List<User> records = page.getRecords();
        System.out.println("total: "+ total);
        System.out.println("records: "+ records);
    }


    // 需求1： 查询id为5的用户信息
    @Test
    public void testGetAll4(){
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getId,5);
        List<User> users = userDao.selectList(qw);
        for (User user : users) {
            System.out.println(user);
        }
    }
    // 需求2： 查询年龄大于等于25的用户信息
    @Test
    public void testGetAll5(){
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.ge(User::getAge,19);
        List<User> users = userDao.selectList(qw);
        for (User user : users) {
            System.out.println(user);
        }
    }
    // 需求3： 查询年龄在20到25之间的用户信息
    @Test
    public void testGetAll6(){
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.between(User::getAge,15,25);
        List<User> users = userDao.selectList(qw);
        for (User user : users) {
            System.out.println(user);
        }
    }
    // 需求4： 查询姓名不为null的用户信息
    @Test
    public void testGetAll7(){
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.isNotNull(User::getName);
        List<User> users = userDao.selectList(qw);
        for (User user : users) {
            System.out.println(user);
        }
    }
    // 需求5： 查询id为2,4,6的用户信息
    @Test
    public void testGetAll8(){
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.in(User::getId,2,4,6);
        List<User> users = userDao.selectList(qw);
        for (User user : users) {
            System.out.println(user);
        }
    }
    // 需求6： 查询姓名以'j'开头的用户信息
    @Test
    public void testGetAll9(){
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.likeRight(User::getName,"j");
        List<User> users = userDao.selectList(qw);
        for (User user : users) {
            System.out.println(user);
        }
    }

    // 条件判断
    // 方式一:
    @Test
    public void testGetAll10(){
        // 模拟前端提交的数据
        User user = new User();
        user.setName("王大帅气");
        user.setAge(23);
        user.setTel("13688886666");
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        // 先对值进行判断, 如果不为空在进行查询
        qw.eq(user.getTel() != null,User::getTel,user.getTel());
        qw.eq(user.getName() != null,User::getName,user.getName());
        qw.eq(user.getAge() != null,User::getAge,user.getAge());
        List<User> users = userDao.selectList(qw);
        for (User user1 : users) {
            System.out.println(user1);
        }
    }

    // 方式二:
    @Test
    public void testGetAll11() {
        // 模拟前端提交的数据
        User user = new User();
        user.setName("王大帅气");
        user.setAge(23);
        user.setTel("13688886666");
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        if (user.getName() != null){
            qw.eq(User::getName,user.getName());
        }
        if (user.getAge() != null){
            qw.eq(User::getAge,user.getAge());
        }
        if (user.getTel() != null){
            qw.eq(User::getTel,user.getTel());
        }
        List<User> users = userDao.selectList(qw);
        for (User user1 : users) {
            System.out.println(user1);
        }
    }

    // 查询指定字段
    @Test
    public void testGetAll12() {
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.select(User::getName,User::getAge);
        List<User> users = userDao.selectList(qw);
        for (User user : users) {
            System.out.println(user);
        }
    }

    // 分组 排序
    @Test
    public void testGetAll13() {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.select("count(*) as count","tel");
        qw.groupBy("tel");
        qw.orderByAsc("count");
        List<Map<String, Object>> maps = userDao.selectMaps(qw);
        System.out.println(maps);
    }

    @Test
    // 修改
    public void testUpdate() {

        // 1. 查询表中的数据，比如对应商品的库存数据
        User user1 = userDao.selectById(2L); //  version=1, count=2
        User user2 = userDao.selectById(2L); //  version=1, count=2

        // 2. 判断是否有库存
        if (user1.getCount() > 0) {
            user1.setCount(user1.getCount() - 1);
            int result = userDao.updateById(user1);
            if (result > 0) {
                System.out.println("user1抢购成功了");
            } else {
                System.out.println("user1抢购失败了");
            }
        }

        if (user2.getCount() > 0) {
            user2.setCount(user2.getCount() - 1);
            int result = userDao.updateById(user2);
            if (result > 0) {
                System.out.println("user2抢购成功了");
            } else {
                System.out.println("user2抢购失败了");
            }
        }
    }
}
